```plaintext
=======================================================================
WELCOME BACK TO REGULAR EXPRESSIONS WITH PYTHON'S RE MODULE: LESSON 4
=======================================================================

Hello again, Regex aficionado! Welcome to Lesson 4 of our exploration into Python's `re` module. We're going to flex your regex muscles with capture groups, named groups, and substitution. Ready? Let's dive in!

As with previous lessons, please start the ipython shell and load the `re` module:

```python
import re
```

=======================================================================
CONCEPT 1: CAPTURE GROUPS FOR EXTRACTION
=======================================================================

Capture groups are parts of your regex surrounded by parentheses `()`. They allow portions of the match to be returned separately.

**Example:** Let's capture first and last names separately.

```python
match = re.search(r'(\w+) (\w+)', 'John Doe')
```

Try accessing `match.group(1)` and `match.group(2)` to see the extracted first and last names.

=======================================================================
EXERCISE 1:
=======================================================================

Capture the day, month, and year components of the date '12-31-2021'.

```python
# Your code here
```

**Expected Outcome:** Extract '12', '31', and '2021' as separate pieces.

=======================================================================
CONCEPT 2: NAMED GROUPS FOR CLARITY
=======================================================================

Named groups are similar to capture groups, but with a name assigned for easier access and readability, using the syntax `(?P<name>...)`.

**Example:** Capture the area code with a name for a phone number format.

```python
match = re.search(r'\((?P<area>\d{3})\)', '(123) 456-7890')
```

Access the group with `match.group('area')`.

=======================================================================
EXERCISE 2:
=======================================================================

Use named groups to capture 'user' and 'domain' parts of an email: 'user@domain.com'.

```python
# Your code here
```

**Expected Outcome:** Use `match.group('user')` and `match.group('domain')` to access these parts.

=======================================================================
CONCEPT 3: SUBSTITUTION WITH re.sub()
=======================================================================

`re.sub(pattern, replacement, string)` replaces matches of the pattern in the string with the replacement.

**Example:** Replace dashes with slashes in a date.

```python
new_date = re.sub(r'-', '/', '2021-12-31')
```

Verify that `new_date` is '2021/12/31'.

=======================================================================
EXERCISE 3:
=======================================================================

Replace spaces with underscores in 'hello world of regex'.

```python
# Your code here
```

**Expected Outcome:** Transform the string to 'hello_world_of_regex'.

=======================================================================
CONCEPT 4: USING GROUPS IN SUBSTITUTION
=======================================================================

Groups can be reused in a replacement string using `\1`, `\2`, etc., or by name with `\g<name>`. This allows rearrangement or alteration during substitution.

**Example:** Swap first and last names in a string.

```python
swapped = re.sub(r'(\w+) (\w+)', r'\2 \1', 'John Doe')
```

Validate that `swapped` outputs 'Doe John'.

=======================================================================
EXERCISE 4:
=======================================================================

Reformat the date '31-12-2021' from 'DD-MM-YYYY' to 'YYYY-MM-DD'.

```python
# Your code here
```

**Expected Outcome:** Change '31-12-2021' to '2021-12-31' using group substitution.

=======================================================================
CHALLENGE:
=======================================================================

Using what you've learned today, write a regex pattern and substitution to reformat the string 'First: John, Last: Doe' into 'Doe, John'.

```python
# Your code here
```

**Success Criteria:** Your output should be correctly formatted as 'Doe, John'.

=======================================================================
FURTHER EXPLORATION:
=======================================================================

- Understand more about backreferences in search patterns.
- Delve into the wider world of regex flags like `re.IGNORECASE`.
- Explore `re.split()` to break strings based on regex patterns.
- Try combining everything you've learned to create complex parsing scripts.

You're mastering some of the more powerful features of regex. Keep experimenting and applying your skills to real-world text processing challenges. Practice makes perfect, and you're doing wonderfully. Until next time, happy regexing!

=======================================================================
```
